package org.moflon.boardgameplatform.controller;

import java.lang.reflect.InvocationTargetException;


import org.eclipse.swt.widgets.Display;
import org.moflon.boardgameplatform.view.Field;
import org.moflon.boardgameplatform.view.View;

import JANGame.Board;
import JANGame.Figure;
import JANGame.Figure0;
import JANGame.Figure1;
import JANGame.GameLogic;
import JANGame.impl.JANGameFactoryImpl;


public class Controller {

	private GameLogic logic;
	private View view;
	
	public Controller(GameLogic logic) {
		this.logic = logic;
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			JANGameFactoryImpl fac = new JANGameFactoryImpl();
			GameLogic logic = fac.createGameLogic();
			Controller controller = new Controller(logic);
			View view = new View(controller, logic.getWidth(), logic.getHeight());
			controller.setView(view);
			view.setBlockOnOpen(true);
			view.open();
			Display.getCurrent().dispose();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setView(View view) {
		this.view = view;
	}

	public void fieldPressed(Field field) {
		logic.fieldPressed(field.getPos().x, field.getPos().y);
		drawBoard(logic.getBoard());
	}

	public void initializeBoard() {
		logic.initialize();
		JANGameFactoryImpl fac = new JANGameFactoryImpl();
		this.createFigure(fac.createFigure3(), "♜", 0);
		this.createFigure(fac.createFigure4(), "♞", 1);
		this.createFigure(fac.createFigure2(), "♝", 2);
		this.createFigure(fac.createFigure5(), "♛", 3);
		this.createFigure(fac.createFigure6(), "♚", 4);
		this.createFigure(fac.createFigure2(), "♝", 5);
		this.createFigure(fac.createFigure4(), "♞", 6);
		this.createFigure(fac.createFigure3(), "♜", 7);
		for (int i = 0; i <= 7; i++) {
			this.createFigure(fac.createFigure0(), "♟", 8+i);
			this.createFigure(fac.createFigure1(), "♙", 48+i);
		}
		this.createFigure(fac.createFigure3(), "♖", 56);
		this.createFigure(fac.createFigure4(), "♘", 57);
		this.createFigure(fac.createFigure2(), "♗", 58);
		this.createFigure(fac.createFigure5(), "♕", 59);
		this.createFigure(fac.createFigure6(), "♔", 60);
		this.createFigure(fac.createFigure2(), "♗", 61);
		this.createFigure(fac.createFigure4(), "♘", 62);
		this.createFigure(fac.createFigure3(), "♖", 63);
		/*
		for (int i = 0; i<49; i++) {
			if (i==24) continue;
			int mod7 = i % 7;
			if (i<14 || i > 34 ) {
				if (mod7 < 2 || mod7 > 4) {
					this.createFigure(fac.createFigure1(), "▩", i);
				} else {
					this.createFigure(fac.createFigure0(), "⎉", i);
				}
			} else {
				this.createFigure(fac.createFigure0(), "⎉", i);
			}
		
		}
		*/
		drawBoard(logic.getBoard());
	}
	
	private void createFigure(Figure fig, String name, int pos) {
		fig.setName(name);
		logic.getBoard().getFields().get(pos).setFigure(fig);
	}
	
	private void drawBoard(Board board) {
		for (JANGame.Field f : board.getFields()) {
			if (f.getFigure() != null) {
				view.getBoard()[f.getX()][f.getY()].setFigure(f.getFigure().getName());
			} else {
				view.getBoard()[f.getX()][f.getY()].clear();
			}
		}
	}
}